package cn.edu.gsau.info.controller;

import cn.edu.gsau.info.exception.BusinessException;
import cn.edu.gsau.info.exception.SystemException;
import cn.edu.gsau.info.models.Code;
import cn.edu.gsau.info.models.ResponseResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.xml.transform.Result;


@RestControllerAdvice
public class ProjectExceptionAdvice {
    //处理业务异常
    @ExceptionHandler(BusinessException.class)
    public ResponseResult doBusinessException(BusinessException ex){
        ex.printStackTrace();
        return new ResponseResult(ex.getCode(),ex.getMessage(),null);
    }
    //处理系统异常
    @ExceptionHandler(SystemException.class)
    public ResponseResult doSystemException(SystemException ex){
        ex.printStackTrace();
        return new ResponseResult(ex.getCode(),ex.getMessage(),null);
    }
    //处理其他异常
    @ExceptionHandler(Exception.class)
    public ResponseResult doOtherException(Exception ex){
        ex.printStackTrace();
        return new ResponseResult(Code.OTHER_EXCEPTION,"系统出现错误，请重试");
    }
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public ResponseResult doMissmatchException(MethodArgumentTypeMismatchException e){
        e.printStackTrace();
        System.out.println("name="+e.getName());
        return new ResponseResult(Code.OTHER_EXCEPTION,"类型转换错误");
    }

}
